#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/2/24
# @Author :cjx
# @File   :io_status.py

import can
import time
import numpy as np
import os
import pymysql


def can_open():  # 开启can通讯
    os.system('sudo ip link set can0 type can bitrate 250000')
    os.system('sudo ifconfig can0 up')


def io_status():
    global var1, var2, data_do, data_di  # 类型为list
    global falling_edge, rising_edge
    global di_location, do_location
    inquiry_do = can.Message(arbitration_id=0x0207, data=[0x01], is_extended_id=False)  # 问询
    bus.send(inquiry_do)  # 0x0201 DO状态 返回513
    message_do = bus.recv(timeout=1)  # 接收数据
    type_id_do = message_do.arbitration_id  # 返回can报文类型
    if type_id_do == 519:
        data_do = '{:0>32b}'.format(int(bin(int.from_bytes(message_do.data[0:4], "little")).replace('0b', ''), 2))[
                  ::-1]
        if var1 != data_do:
            var1 = data_do
            do_location = list(data_do)
            time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    time.sleep(0.1)
    print(do_location)
    inquiry_di = can.Message(arbitration_id=0x0308, data=[0x01], is_extended_id=False)
    bus.send(inquiry_di)  # 0x0301  返回769/0x0301 di状态
    message_di = bus.recv(timeout=1)  # 接收数据
    type_id_di = message_di.arbitration_id  # 返回can报文类型
    if type_id_di == 776:
        data_di = '{:0>32b}'.format(int(bin(int.from_bytes(message_di.data[0:4], "little")).replace('0b', ''), 2))[
                  ::-1]
        if data_di != var2:  # 差异检测，差异写入数据库
            for k in range(32):
                if var2[k] == '0' and data_di[k] == '1':  # 32点上升沿检测,没有检测到不复位
                    edge[0, k] = 1  # 写入边缘检测edge[0]
                    edge[1, k] = 0  # 复位下降沿
                if var2[k] == '1' and data_di[k] == '0':  # 32点下降沿检测,没有检测到不复位
                    edge[1, k] = 1  # 写入边缘检测edge[1]
                    edge[0, k] = 0  # 复位上升沿
            var2 = data_di  # 给中间值赋新值
            di_location = list(data_di)  # 字符串转列表输出
            time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    time.sleep(0.1)
    print(di_location)


if __name__ == "__main__":
    # message_di = 0
    global manual_control_status, remote_maintenance_status, automatic_cleaning_status, do_location
    os.system('sudo ifconfig can0 down')
    can_open()
    var1, var2 = '00000000000000000000000000000000', '00000000000000000000000000000000'
    data_do, data_di = 0, 0
    do_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    di_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    falling_edge = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                    '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    rising_edge = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    edge = np.zeros(64, np.int8).reshape(2, 32)  # DI上升/下降沿检测,edge[0,0]第1个up，edge[1,0]第1个down
    bus = can.Bus(interface='socketcan', channel='can0', receive_own_messages=eval('False'))
    while 1:
        io_status()
        time.sleep(1)
